const API = require('../../../utils/api')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type:['发货问题','售后问题','服务问题','账号问题','系统问题','意见反馈'],
        typeCurent:'',
        uploadImgList:[],
        form:{
            userId:'', 
            issue:'发货问题', //反馈问题
            content:'',//反馈内容
            picUrls:'' //图片
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    //事件 - 问题分类 type 改变
    typeChange:function(e){
        console.log(e.currentTarget.id)
        this.setData({
            typeCurent:e.currentTarget.id,
            ['form.issue']: this.data.type[e.currentTarget.id]
        })
    },

    /**
     * 事件 - 上传图片
     */
    uploadImg:function(){
        let that = this
        wx.chooseImage({
          count: 1,
          sizeType:'compressed',
          success:function(res){
            //   console.log( '1. 选择图片', res )
            that.public_uploadimg( res.tempFiles[0].path, get_img_data )
            let uploadImgList = that.data.uploadImgList
            function get_img_data(data){
                console.log('获取上传返回的数据',data)
                if( data.code == 200 ){
                    uploadImgList.push(data.data.url)
                    that.setData({
                        uploadImgList:uploadImgList
                    })
                }
            }
          }
        })
    },

    /**
     * 事件 - 删除已上传的图片
     */
    deleteUploadImg:function(e){
        // console.log(e.currentTarget.id)
        let uploadImgList = this.data.uploadImgList
        uploadImgList.splice(e.currentTarget.id,1)
        this.setData({
            uploadImgList
        })
    },

    /**
     * 事件 - 表单提交
     */
    submit:function(e){
        // console.log('表单',e.detail.value)
        let form = this.data.form
        form.content = e.detail.value.content
        form.userId = app.globalData.userInfo.id
        form.picUrls = this.data.uploadImgList
        console.log( form )
        
        //表单验证
        if( !form.content ){
            wx.showToast({
              title: '请填写意见反馈',
              icon:'none'
            })
            return
        }

        API.creat_feecback(form).then( res => {
            if( res.code == 200 ){
                wx.showToast({
                  title: '提交成功',
                })
                setTimeout(() => {
                    wx.navigateBack({
                      delta: 1,
                    })
                }, 1500);
            }else{
                wx.showToast({
                    title: '提交失败，请稍后重试',
                    icon:'none'
                })
            }
        })
    },

    /**
     * 公共上传图片接口
     */
    public_uploadimg:function(file,callback){
        console.log('baseurl',API.getHost())
        wx.showLoading({
          title: '上传中',
        })
        wx.uploadFile({
            url: API.getHost() + '/wx/appletFeedback/uploadimg', 
            header: {
                Authorization: 'wx' + wx.getStorageSync('token')
            },
            filePath: file,
            name: 'file',
            formData: {
              'file': file
            },
            success (res){
              const data = res.data
              callback(JSON.parse(data))
              console.log('成功上传',JSON.parse(data))
              wx.hideLoading()
            },
            fail:function(){
                wx.hideLoading()
                wx.showToast({
                  title: '上传失败，请稍后重试',
                  icon:'none'
                })
            }
        })
    },
})